/*
 * Copyright 2010 Alessandro Bellucci
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.soupframework.gwt.designerpanel.client.util;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Widget;

public class Util {

	private Util(){
	}

	/**
	 * Checks whether the widget contains the specified coords.
	 *
	 * @param w The widget
	 * @param x Coordinate x
	 * @param y Coordinate y
	 * @return true if the widgets contains coordinates (x,y), false otherwise
	 */
	public static boolean widgetContainsCoords(Widget w, int x, int y){
		int left = w.getAbsoluteLeft() - getTotalWidgetScrollLeft(w);
		int top = w.getAbsoluteTop() - getTotalWidgetScrollTop(w);
		int right = left + w.getOffsetWidth();
		int bottom = top + w.getOffsetHeight();
		return x >= left && x <= right && y >= top && y<= bottom;
	}

	/**
	 * Checks whether the widget intersects the specified area.
	 *
	 * @param w The widget
	 * @param startX Start coordinate x
	 * @param startY Start coordinate y
	 * @param endX End coordinate x
	 * @param endY End coordinate y
	 * @return true if the widget intersects the specified area, false otherwise
	 */
	public static boolean widgetIntersectsArea(Widget w, int startX, int startY, int endX, int endY){
		int wLeft = w.getAbsoluteLeft() - getTotalWidgetScrollLeft(w);
		int wTop = w.getAbsoluteTop() - getTotalWidgetScrollTop(w);
		int wRight = wLeft + w.getOffsetWidth();
		int wBottom = wTop + w.getOffsetHeight();

		int aLeft = Math.min(startX, endX);
		int aTop = Math.min(startY, endY);
		int aRight = Math.max(startX, endX);
		int aBottom = Math.max(startY, endY);

		if(wLeft > aRight) return false;
		if(wRight < aLeft) return false;
		if(wTop > aBottom) return false;
		if(wBottom < aTop) return false;

		return true;
	}

	public static int getTotalWidgetScrollLeft(Widget w){
		int totalScrollLeft = 0;
		/* this loop behaves in a different way in (chrome) and (firefox and ie):
		 * chrome gets a positive scroll for root panel,firefox and ie do not
		while(w != null){
			totalScrollLeft += w.getElement().getScrollLeft();
			w = w.getParent();
		}*/
		totalScrollLeft += Window.getScrollLeft();
		return totalScrollLeft;
	}

	public static int getTotalWidgetScrollTop(Widget w){
		int totalScrollTop = 0;
		/* this loop behaves in a different way in (chrome) and (firefox and ie):
		 * chrome gets a positive scroll for root panel,firefox and ie do not
		while(w != null){
			totalScrollTop += w.getElement().getScrollTop();
			w = w.getParent();
		}*/
		totalScrollTop += Window.getScrollTop();
		return totalScrollTop;
	}

	public static Integer getWidgetStyleLeft(Widget w){
		return w.getElement().getStyle() != null ? getStyleValueInPixel(w.getElement().getStyle().getLeft()) : null;
	}

	public static Integer getWidgetStyleTop(Widget w){
		return w.getElement().getStyle() != null ? getStyleValueInPixel(w.getElement().getStyle().getTop()) : null;
	}

	public static Integer getWidgetStyleWidth(Widget w){
		return w.getElement().getStyle() != null ? getStyleValueInPixel(w.getElement().getStyle().getWidth()) : null;
	}

	public static Integer getWidgetStyleHeight(Widget w){
		return w.getElement().getStyle() != null ? getStyleValueInPixel(w.getElement().getStyle().getHeight()) : null;
	}

	public static Integer getStyleValueInPixel(String styleValue){
		if(styleValue == null || styleValue.length() < 3){
			return null;
		}
		String suffix = styleValue.substring(styleValue.length() - 2, styleValue.length());
		if(!suffix.equalsIgnoreCase("px")){
			return null;
		}
		return new Integer(styleValue.substring(0, styleValue.length() - 2));
	}
}
